// Registry wrapper
// Copyright 2006 (C) Ralph Thomas

#include <util/registry.h>
#include <gconf/gconf-client.h>

namespace util {
	adobe::value_t registry_get( const std::string& key ) {
		GConfClient* client( gconf_client_get_default() );
		if( !client ) { g_object_unref( client ); return adobe::value_t(); }
		GConfValue* value( gconf_client_get( client, key.c_str(), 0 ) );
		if( !value ) { g_object_unref( client ); return adobe::value_t(); }
		adobe::value_t out;
		switch( value->type ) {
		  case GCONF_VALUE_STRING:
			  out = adobe::value_t( std::string( gconf_value_get_string( value ) ) );
			  break;
		  case GCONF_VALUE_INT:
			  out = adobe::value_t( gconf_value_get_int( value ) );
			  break;
		  case GCONF_VALUE_FLOAT:
			  out = adobe::value_t( gconf_value_get_float( value ) );
			  break;
		  case GCONF_VALUE_BOOL:
			  out = adobe::value_t( gconf_value_get_bool( value ) ? true : false );
			  break;
		}
		gconf_value_free( value );
		return out;
	}
	void registry_set( const std::string& key, const adobe::value_t& value ) {
		GConfValue* gvalue( 0 );
		if( value.type() == typeid( std::string ) ) {
			std::string s = value.get<std::string>();
			gvalue = gconf_value_new_from_string( GCONF_VALUE_STRING, s.c_str(), 0 );
		} else if( value.type() == typeid( double ) ) {
			double d = value.get<double>();
			gvalue = gconf_value_new( GCONF_VALUE_FLOAT );
			gconf_value_set_float( gvalue, d );
		} else if( value.type() == typeid( bool ) ) {
			bool b = value.get<bool>();
			gvalue = gconf_value_new( GCONF_VALUE_BOOL );
			gconf_value_set_bool( gvalue, b );
		} else return;
		//
		// Set the value into the client.
		//
		GConfClient* client( gconf_client_get_default() );
		gconf_client_set( client, key.c_str(), gvalue, 0 );
		g_object_unref( client );
		gconf_value_free( gvalue );
	}
}

